//
//  WarkCollectionHeadView.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/4/24.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "WarkCollectionHeadView.h"

@implementation WarkCollectionHeadView
- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.chooseArray=[[NSMutableDictionary alloc]init];
        self.backgroundColor=Gray_Color;
        UIView*topView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, MainW, 140)];
        topView.backgroundColor=[UIColor whiteColor];
        [self addSubview:topView];
        MYLabel *label=[[MYLabel alloc]initWithFrame:CGRectMake(10, 20, MainW-20, 15)];
        label.text=@"请选择类别";
        label.textColor=TextColor3;
        [topView addSubview:label];
        int totalColumns = 4;
        
        //       每一格的尺寸
        CGFloat cellW = MainW/4-30;
        CGFloat cellH = 25;
        
        //    间隙
        CGFloat margin =15;
        
        //    根据格子个数创建对应的框框
        for(int index = 0; index< 8; index++) {
            UIButton *btn=[[UIButton alloc] init];
            [btn setTitle:@"重金求子" forState:UIControlStateNormal];
            btn.titleLabel.font=FontAndStyle12;
            [btn setTitleColor:TextColor3 forState:UIControlStateNormal];
            [btn setTitleColor:button_Red_Color forState:UIControlStateSelected];
            [btn setBackgroundColor:HJ_Color(229, 229, 229)];
            [btn setTag:index];
            [btn addTarget:self action:@selector(fourBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            // 计算行号  和   列号
            int row = index / totalColumns;
            int col = index % totalColumns;
            //根据行号和列号来确定 子控件的坐标
            CGFloat cellX = 35 + col * (cellW + margin);
            CGFloat cellY = 55+row * (cellH + margin);
            btn.frame = CGRectMake(cellX, cellY, cellW, cellH);
            ViewRadius(btn, 2);
            
            // 添加到view 中  
            [topView addSubview:btn];
        }
        MYLabel *wlabel=[[MYLabel alloc]init];
        wlabel.textColor=TextColor3;
        wlabel.text=@"请在这里写下您的心愿";
        wlabel.backgroundColor=[UIColor clearColor];
        [self addSubview:wlabel];
        self.textView=[[UITextView alloc]init];
        self.textView.delegate=self;
        [self addSubview:self.textView];
        ViewRadius(self.textView, 2);
        
        self.MySureBtn=[[UIButton alloc]init];
        [self.MySureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.MySureBtn setTitle:MyString(@"递交佛祖") forState:UIControlStateNormal];
        [self.MySureBtn addTarget:self action:@selector(NextSureClick) forControlEvents:UIControlEventTouchUpInside];
        [self.MySureBtn setBackgroundColor:Red_Color];
        [self addSubview:self.MySureBtn];
        [self.MySureBtn setTitleColor:TextColor9 forState:UIControlStateNormal];
        [self.MySureBtn setBackgroundColor:SeparatorColor];
        [self.MySureBtn setEnabled:NO];
        ViewRadius(self.MySureBtn, 5);
        [wlabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(topView.mas_bottom).offset(20);
            make.left.equalTo(10);
            make.right.equalTo(-10);
            make.height.equalTo(15);
        }];
        [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(wlabel.mas_bottom).offset(20);
            make.left.equalTo(10);
            make.right.equalTo(-10);
            make.height.equalTo(MainW/3);
        }];
        [self.MySureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.textView.mas_bottom).offset(20);
            make.left.equalTo(10);
            make.right.equalTo(-10);
            make.height.equalTo(49);
        }];

        

        
    }
    return self;
}
-(void)fourBtnClick:(UIButton *)sender{
    [self.textView endEditing:YES];
    sender.selected=!sender.selected;
    NSString*key=[NSString stringWithFormat:@"%ld",(long)sender.tag
                  ];
    if (sender.selected==YES) {
        [sender setBackgroundColor:[UIColor whiteColor]];
        sender.layer.borderWidth=1;
        sender.layer.borderColor=button_Red_Color.CGColor;
        [self.chooseArray setValue:[NSString stringWithFormat:@"%ld",(long)sender.tag
                                    ] forKey:key];
    }else{
        [sender setBackgroundColor:HJ_Color(229, 229, 229)];
        sender.layer.borderWidth=0.01;
        sender.layer.borderColor=[UIColor whiteColor].CGColor;
        [self.chooseArray removeObjectForKey:key];
    }
    //NSString *value = [self.chooseArray objectForKey:key];
    if (self.chooseArray.count==0)
    {
        if (self.textView.text.length==0) {
            [self.MySureBtn setTitleColor:TextColor9 forState:UIControlStateNormal];
            [self.MySureBtn setBackgroundColor:SeparatorColor];
            [self.MySureBtn setEnabled:NO];
        }else{
            NSLog(@"");
        }
        
    }
    else
    {
        [self.MySureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.MySureBtn setBackgroundColor:Red_Color];
        [self.MySureBtn setEnabled:YES];
    }
    [_delgeate chooseClassesClick:sender Choose:self.chooseArray];
}
-(void)NextSureClick{
    [_delgeate submitClick:self.chooseArray NSString:self.textView.text];
    //NSLog(@"@%@",self.chooseArray);
}
- (void)textViewDidChange:(UITextView *)textView{
    NSLog(@"这是一-*****1");
    if (textView.text.length==0) {
        [self.MySureBtn setTitleColor:TextColor9 forState:UIControlStateNormal];
        [self.MySureBtn setBackgroundColor:SeparatorColor];
        [self.MySureBtn setEnabled:NO];
    }else{
        [self.MySureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.MySureBtn setBackgroundColor:Red_Color];
        [self.MySureBtn setEnabled:YES];
    }
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
    //[textView endEditing:YES];
    return YES;
}
@end
